<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入id" suffix-icon="el-icon-search" v-model="pbId"></el-input>
      <el-input style="width: 200px" placeholder="请输入投放点id" suffix-icon="el-icon-search" v-model="pbssId" class="ml-5"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>
    <div style="margin: 10px 0">
      <!--      <el-upload action="http://localhost:9090/file/upload" :show-file-list="false" :on-success="handleFileUploadSuccess" style="display: inline-block">-->
      <!--        <el-button type="primary" class="ml-5">上传文件 <i class="el-icon-top"></i></el-button>-->
      <!--      </el-upload>-->
      <el-button type="primary" @click="handleAdd">新增<i class="el-icon-circle-plus-outline"></i></el-button>
      <el-popconfirm
          class="ml-5"
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="您确定批量删除这些数据吗？"
          @confirm="delBatch"
      >
        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>

    </div>
    <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="pbId" label="pbId" width="80"></el-table-column>
      <el-table-column prop="pbName" label="充电宝名称"></el-table-column>
      <el-table-column prop="pbssId" label="所属投放点id"></el-table-column>
      <el-table-column prop="pbssName" label="所属投放点名称"></el-table-column>
      <el-table-column prop="pbPrice" label="价格"></el-table-column>
      <el-table-column prop="pbStatus" label="租借状态" :formatter="formatBoolean" :show-overflow-tooltip="true">
        <template slot-scope="scope">
<!--          {{scope.row.pbStatus ? "已租借":"未租借"}}-->
          <el-tag type="danger" v-if="scope.row.pbStatus==true">
            已租借
          </el-tag>
          <el-tag type="success" v-if="scope.row.pbStatus==false">
            未租借
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="启用">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.enable" active-color="#13ce66" inactive-color="#ccc" @change="changeEnable(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作"  width="200" align="center">
        <template slot-scope="scope">
          <el-button type="success" @click="handleEdit(scope.row)">编辑<i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="del(scope.row.pbId)"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog title="充电宝信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" label-width="80px">
        <el-form-item label="充电宝名称" >
          <el-input v-model="form.pbName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="所属投放点名称" >
<!--          <el-input v-model="form.pbssName" autocomplete="off"></el-input>-->
          <template>
            <el-select v-model="form.pbssName" placeholder="请选择">
              <el-option
                  v-for="(item,index) in options"
                  :key="index"
                  :label="item"
                  :value="item">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="租借状态" >
          <el-input v-model="form.pbStatus" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="价格" >
          <el-input v-model="form.pbPrice" autocomplete="off"></el-input>
        </el-form-item>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="save" >确 定</el-button>

      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "Pb",
  data() {
    return {
      tableData: [],
      pbId:'',
      pbssId:'',
      multipleSelection: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      dialogFormVisible:false,
      form:{},
      options:[]
    }
  },
  created() {
    this.load(),
        this.getPbssName()
  },
  methods: {
     load() {
      this.request.get("/Pb/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          pbssId:this.pbssId,
          pbId:this.pbId

        }
      }).then(res => {
         if(res.code==='200'){
           this.tableData = res.data.records
           this.total = res.data.total
         }else {
           // this.$message.error(res.msg)
         }


      })
    },
    save(){
      this.request.post("/Pb/save",this.form).then(res=>{
        if (res){
          this.$message.success("保存成功！")
          this.dialogFormVisible=false;
          this.load()
        }else {
          this.$message.error("保存失败")
        }
      })
    },
    changeEnable(row) {
      this.request.post("/Pb/update", row).then(res => {
        if (res.code === '200') {
          this.$message.success("操作成功")
        }
      })
    },
    del(id) {
      this.request.delete("/Pb/"+id).then(res => {
        if (res.code === '200') {
          this.$message.success("删除成功")
          this.load()
        } else if(res.code==='401'){

        }else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.pbId)  // [{}, {}, {}] => [1,2,3]
      this.request.post("/Pb/del/batch",ids).then(res => {
        if (res.code === '200') {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          // this.$message.error("批量删除失败")
        }
      })
    },
    reset() {
      this.pbssName = "",
          this.pbssId=''
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
    handleAdd(){
      this.dialogFormVisible=true,
          this.form={}

    },
    handleEdit(row){
      this.dialogFormVisible=true,
          this.form=row
    },
    getPbssName(){
      this.request.get("Pbss/pbssName").then(res=>{
        console.log(res)
        this.options=res.data
      })
    }
    // handleFileUploadSuccess(res) {
    //   console.log(res)
    //   this.load()
    // },
    // download(url) {
    //   window.open(url)
    // }
    // formatBoolean: function (row) {
    //   let ret = ''
    //   if (row.sendStatus === true) {
    //     ret = "已租借" //根据自己的需求设定
    //   } else {
    //     ret = "未租借"
    //   }
    //   return ret;
    // }

  }
}
</script>

<style scoped>

</style>